package com.gz.EuchreU.Controls;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;

import com.gz.EuchreU.Contants.CardValues;
import com.gz.EuchreU.Contants.GameOptions;
import com.gz.EuchreU.Contants.Suits;
import com.gz.EuchreU.Contants.UI;
import com.gz.EuchreU.Sprite.Card;

public class OptionsMenu extends MovingMenuEntity{

	private RatingCards mPlaySpeed;
	private Text mPlaySpeedText;
	private RatingCards mPauseLength;
	private Text mPauseText;
	private Card mAutoLastCard;
	private Text mAutoLastText;
	private Card mAuto31Card;
	private Text mAuto31Text;
	private Card mShowCardsCard;
	private Text mShowCardsText;
	
	public OptionsMenu(final float pX, final float pY, final float pWidth, Scene pScene){
		super(pX,pY, pWidth, true);
		float cardWidth = pWidth/15;
		float buttonX = pX + pWidth/2;
		float buttonY = pY + 30;
		float textX = pX + pWidth/4 - cardWidth;
			
		//mPlaySpeedCard = GetAceOfSpades(buttonX, buttonY, cardWidth);
		mPlaySpeed = new RatingCards(buttonX, buttonY,cardWidth,pScene){

			@Override
			public void OnValueChanged(int pNewValue) {
				GameOptions.SetIntOption(GameOptions.PREF_INT_GAME_SPEED, pNewValue);			
			}
			
		};
		float cardHeight = UI.getTextures().getHeightFromWidthTiled(0, 0, cardWidth);
		float textY = buttonY + cardHeight/3;		
		mPlaySpeedText = GetText(textX, textY, "Play Speed");
		textY += cardHeight + 2;
		buttonY += cardHeight + 2;
		//mPauseCard = GetAceOfSpades(buttonX, buttonY, cardWidth);
		mPauseLength = new RatingCards(buttonX, buttonY, cardWidth, pScene){

			@Override
			public void OnValueChanged(int pNewValue) {
				GameOptions.SetIntOption(GameOptions.PREF_INT_PAUSE_LENGTH, pNewValue);
			}
			
		};
		mPauseText = GetText(textX, textY, "Pause after trick");
		textY += cardHeight + 2;
		buttonY += cardHeight + 2;
		mAutoLastCard = GetAceOfSpades(buttonX, buttonY, cardWidth);
		mAutoLastText = GetText(textX, textY, "Auto Play Last");
		textY += cardHeight + 2;
		buttonY += cardHeight + 2;
		mAuto31Card  = GetAceOfSpades(buttonX, buttonY, cardWidth);
		mAuto31Text = GetText(textX, textY, "Auto Play at 3-1");
		textY += cardHeight + 2;
		buttonY += cardHeight + 2;
		mShowCardsCard = GetAceOfSpades(buttonX, buttonY, cardWidth);
		mShowCardsText = GetText(textX, textY, "Show AI cards");
		
		attachChild(mPlaySpeed);
		attachChild(mPlaySpeedText);
		attachChild(mPauseLength);
		attachChild(mPauseText);
		attachChild(mAutoLastCard);
		attachChild(mAutoLastText);
		attachChild(mAuto31Card);
		attachChild(mAuto31Text);
		attachChild(mShowCardsCard);
		attachChild(mShowCardsText);
		
		pScene.registerTouchArea(mAutoLastCard);
		pScene.registerTouchArea(mAuto31Card);
		pScene.registerTouchArea(mShowCardsCard);
		
		mPlaySpeed.setValue(GameOptions.GetIntOption(GameOptions.PREF_INT_GAME_SPEED), false);
		mPauseLength.setValue(GameOptions.GetIntOption(GameOptions.PREF_INT_PAUSE_LENGTH), false);
		mAutoLastCard.setDisabled(!GameOptions.GetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_LAST));
		if(GameOptions.GetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_LAST))mAuto31Card.setDisabled(false);
		else mAuto31Card.setDisabled(!GameOptions.GetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_31));
		mShowCardsCard.setDisabled(!GameOptions.GetBoolOption(GameOptions.PREF_BOOL_SHOW_AI));
	}
	
	@Override
	public Card GetAceOfSpades(final float pX, final float pY, final float pWidth){
		return new Card(pX,pY,pWidth,Suits.SPADES,CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){		
					
					if(this.getCardID() == mAutoLastCard.getCardID()){
						this.setDisabled(!this.getDisabled());
						mAuto31Card.setDisabled(this.getDisabled());
						
						GameOptions.SetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_LAST, !this.getDisabled());
						GameOptions.SetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_31, !mAuto31Card.getDisabled());
					}
					else if(this.getCardID() == mAuto31Card.getCardID()){
						if(mAutoLastCard.getDisabled()){
							this.setDisabled(!this.getDisabled());
							GameOptions.SetBoolOption(GameOptions.PREF_BOOL_AUTO_PLAY_31, !mAuto31Card.getDisabled());
						}
					}
					else if(this.getCardID() == mShowCardsCard.getCardID()){
						this.setDisabled(!this.getDisabled());
						GameOptions.SetBoolOption(GameOptions.PREF_BOOL_SHOW_AI, !this.getDisabled());
					}
				}
				return true;
			}
		};
	}
	
}
